package com.dufang.string01;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;

/**
 * 日期方式
 */
public class DemoPeriodAndDuration {
    public static void main(String[] args) {
        //plus:向后偏移    minus:向前偏移
        LocalDate localDate=LocalDate.now();
        LocalDate localDate1=localDate.plusYears(1L);
        System.out.println("plus向后偏移：localDate1= "+localDate1);
        localDate1=localDate.minusYears(2L);
        System.out.println("minus向前偏移：localDate1= "+localDate1);


        LocalDate local1=LocalDate.of(2022,12,10);
        LocalDate local2=LocalDate.of(2024,12,10);

        //计算两个日期之间的差值
        Period period=Period.between(local1,local2);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());

        LocalDateTime localDateTime1=LocalDateTime.of(2022,10,22,10,10,10);
        LocalDateTime localDateTime2=LocalDateTime.of(2024,10,22,10,10,10);
        //获取相差的时分秒
        Duration duration=Duration.between(localDateTime1,localDateTime2);
        System.out.println(duration.toDays());
        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());
        System.out.println(duration.toMillis());
    }
}
